package com.xuecheng.content.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * 第三方 RestTemplate 配置类
 *
 * @author Lily
 * @date 2021/12/13 15:18
 * @paramType
 * @return
 * @description
 */
@Configuration
public class RestTemplateConfig {


    /**
     * 创建 RestTemplate 示例类并注入到 Spring 容器中
     *
     * @param factory 工厂
     * @return org.springframework.web.client.RestTemplate
     * @author Lily
     * @date 2021/12/13 15:28
     * @paramType [org.springframework.http.client.ClientHttpRequestFactory]
     * @description
     */
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }


    /**
     * 创建 Http 连接工厂，配置连接时长和读取时长
     * @return
     * TODO 这里要打注解，因为上面需要调用----更正：可以不打注解，因为默认根据类型装配
     */
    @Bean("factory")
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(15000);
        factory.setReadTimeout(5000);
        return factory;
    }
}
